package com.ruoyi.iot.dataprocessing.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.PrimaryKeyIdUtils;
import com.ruoyi.iot.api.domain.HealthDatas;
import com.ruoyi.iot.api.domain.HeathParam;
import com.ruoyi.iot.api.domain.TimeValueItem;
import com.ruoyi.iot.dataprocessing.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.iot.dataprocessing.mapper.SensorMessageMapper;
import com.ruoyi.iot.dataprocessing.service.ISensorMessageService;

/**
 * 设备消息Service业务层处理
 *
 * @author luogongquan
 * @date 2024-08-26
 */
@Service
public class SensorMessageServiceImpl implements ISensorMessageService {
    @Autowired
    private SensorMessageMapper sensorMessageMapper;

    /**
     * 查询设备消息
     *
     * @param id 设备消息主键
     * @return 设备消息
     */
    @Override
    public SensorMessage selectSensorMessageById(Long id) {
        return sensorMessageMapper.selectSensorMessageById(id);
    }

    /**
     * 查询设备消息列表
     *
     * @param sensorMessage 设备消息
     * @return 设备消息
     */
    @Override
    public List<SensorMessage> selectSensorMessageList(SensorMessage sensorMessage) {
        return sensorMessageMapper.selectSensorMessageList(sensorMessage);
    }

    /**
     * 新增设备消息
     *
     * @param sensorMessage 设备消息
     * @return 结果
     */
    @Override
    public void insertSensorMessage(SensorMessage sensorMessage) {

        sensorMessage.setId(PrimaryKeyIdUtils.getSysIdWorker().nextId());
        sensorMessage.setCreateTime(DateUtils.getNowDate());
        sensorMessageMapper.insertSensorMessage(sensorMessage);
    }

    /**
     * 修改设备消息
     *
     * @param sensorMessage 设备消息
     * @return 结果
     */
    @Override
    public int updateSensorMessage(SensorMessage sensorMessage) {
        sensorMessage.setMessage(sensorMessage.getMessageStr().getBytes());
        return sensorMessageMapper.updateSensorMessage(sensorMessage);
    }

    /**
     * 批量删除设备消息
     *
     * @param ids 需要删除的设备消息主键
     * @return 结果
     */
    @Override
    public int deleteSensorMessageByIds(Long[] ids) {
        return sensorMessageMapper.deleteSensorMessageByIds(ids);
    }

    /**
     * 删除设备消息信息
     *
     * @param id 设备消息主键
     * @return 结果
     */
    @Override
    public int deleteSensorMessageById(Long id) {
        return sensorMessageMapper.deleteSensorMessageById(id);
    }

    public HealthDatas getHealthDataByDay(Long customerId) {
        // 获取当前日期，只需要年月日
        LocalDateTime startOfDay = LocalDateTime.now().toLocalDate().atStartOfDay();
        Date date = Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
        Date date1 = DateUtils.addDays(date, -1);
        List<SensorMessage> sensorMessages = sensorMessageMapper.getDataByCustomerId(customerId, date1, date);
        if (sensorMessages.isEmpty()) {
            return null;
        }
        HealthDatas healthDatas = new HealthDatas();
        healthDatas.setBloodSugars(new ArrayList<TimeValueItem>());
        healthDatas.setHeartRates(new ArrayList<TimeValueItem>());
        healthDatas.setOxygens(new ArrayList<TimeValueItem>());
        healthDatas.setBloodPressures(new ArrayList<TimeValueItem>());
        for (int i = 0; i < sensorMessages.size(); i++) {
            SensorMessage sensorMessage = sensorMessages.get(i);
            String messageStr = sensorMessage.getMessageStr();
            WatchMessage watchMessage = JSONObject.parseObject(messageStr, WatchMessage.class);
            WatchMessage.DeviceData data = watchMessage.getData();
            Date tsStart = sensorMessage.getTsStart();
            // 获取tsStart的时分
            String time = DateUtils.parseDateToStr("HH:mm", tsStart);
            if (!data.getBloodGlucose().equals(0.0d)) {
                TimeValueItem bloodSugar = new TimeValueItem();
                bloodSugar.setLabel(time);
                bloodSugar.setSortOrder(i);
                bloodSugar.setValue1(data.getBloodGlucose());
                healthDatas.getBloodSugars().add(bloodSugar);
            }

            if (!data.getHighValue().equals(0)) {
                TimeValueItem bloodPressure = new TimeValueItem();
                bloodPressure.setLabel(time);
                bloodPressure.setSortOrder(i);
                bloodPressure.setValue1(data.getHighValue());
                bloodPressure.setValue2(data.getLowValue());
                healthDatas.getBloodPressures().add(bloodPressure);
            }

            List<Integer> oxygens = data.getOxygens();
            for (int i1 = 0; i1 < oxygens.size(); i1++) {
                TimeValueItem oxygen = new TimeValueItem();
                Date date2 = DateUtils.addMinutes(tsStart, i1);
                oxygen.setLabel(DateUtils.parseDateToStr("HH:mm", date2));
                oxygen.setValue1(oxygens.get(i1));
                oxygen.setSortOrder(i * oxygens.size() + i1);
                healthDatas.getOxygens().add(oxygen);
            }

            List<Integer> ppgs = data.getPpgs();
            for (int i1 = 0; i1 < ppgs.size(); i1++) {
                TimeValueItem ppg = new TimeValueItem();
                Date date2 = DateUtils.addMinutes(tsStart, i1);
                ppg.setLabel(DateUtils.parseDateToStr("HH:mm", date2));
                ppg.setValue1(ppgs.get(i1));
                ppg.setSortOrder(i * ppgs.size() + i1);
                healthDatas.getHeartRates().add(ppg);
            }
        }
        return healthDatas;

    }

    public HealthDatas getHealthData(HeathParam healthParam) {

        // 获取当前日期，只需要年月日
        LocalDateTime localDateTime = healthParam.getDate().toInstant().atZone(ZoneId.systemDefault())
                .toLocalDateTime();
        LocalDateTime startOfDay = localDateTime.toLocalDate().atStartOfDay();
        Date date = Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant());
        Date date1 = DateUtils.addDays(date, -1);
        List<SensorMessage> sensorMessages = sensorMessageMapper.getDataByCustomerId(healthParam.getCustomerId(), date1,
                date);
        if (sensorMessages.isEmpty()) {
            return null;
        }
        HealthDatas healthDatas = new HealthDatas();
        healthDatas.setBloodSugars(new ArrayList<TimeValueItem>());
        healthDatas.setHeartRates(new ArrayList<TimeValueItem>());
        healthDatas.setOxygens(new ArrayList<TimeValueItem>());
        healthDatas.setBloodPressures(new ArrayList<TimeValueItem>());
        for (int i = 0; i < sensorMessages.size(); i++) {
            SensorMessage sensorMessage = sensorMessages.get(i);
            String messageStr = sensorMessage.getMessageStr();
            WatchMessage watchMessage = JSONObject.parseObject(messageStr, WatchMessage.class);
            WatchMessage.DeviceData data = watchMessage.getData();
            Date tsStart = sensorMessage.getTsStart();
            // 获取tsStart的时分
            String time = DateUtils.parseDateToStr("HH:mm", tsStart);
            if (!data.getBloodGlucose().equals(0.0d)) {
                TimeValueItem bloodSugar = new TimeValueItem();
                bloodSugar.setLabel(time);
                bloodSugar.setSortOrder(i);
                bloodSugar.setValue1(data.getBloodGlucose());
                healthDatas.getBloodSugars().add(bloodSugar);
            }

            if (!data.getHighValue().equals(0)) {
                TimeValueItem bloodPressure = new TimeValueItem();
                bloodPressure.setLabel(time);
                bloodPressure.setSortOrder(i);
                bloodPressure.setValue1(data.getHighValue());
                bloodPressure.setValue2(data.getLowValue());
                healthDatas.getBloodPressures().add(bloodPressure);
            }

            List<Integer> oxygens = data.getOxygens();
            for (int i1 = 0; i1 < oxygens.size(); i1++) {
                TimeValueItem oxygen = new TimeValueItem();
                Date date2 = DateUtils.addMinutes(tsStart, i1);
                oxygen.setLabel(DateUtils.parseDateToStr("HH:mm", date2));
                oxygen.setValue1(oxygens.get(i1));
                oxygen.setSortOrder(i * oxygens.size() + i1);
                healthDatas.getOxygens().add(oxygen);
            }

            List<Integer> ppgs = data.getPpgs();
            for (int i1 = 0; i1 < ppgs.size(); i1++) {
                TimeValueItem ppg = new TimeValueItem();
                Date date2 = DateUtils.addMinutes(tsStart, i1);
                ppg.setLabel(DateUtils.parseDateToStr("HH:mm", date2));
                ppg.setValue1(ppgs.get(i1));
                ppg.setSortOrder(i * ppgs.size() + i1);
                healthDatas.getHeartRates().add(ppg);
            }
        }
        return healthDatas;

    }

    @Override
    public WatchVO getLastMessageByCustomerId(Long customerId) {
        WatchVO watchVO = new WatchVO();
        List<SensorMessage> list = sensorMessageMapper.getLastMessageByCustomerId(customerId);
        if (list.isEmpty()) {
            return watchVO;
        } else if (list.size() == 1) {
            String messageStr = list.get(0).getMessageStr();
            WatchMessage watchMessage = JSONObject.parseObject(messageStr, WatchMessage.class);
            WatchMessage.DeviceData data = watchMessage.getData();
            watchVO.setHeartRate(data.getPpgs().get(4));
            watchVO.setOxygen(data.getOxygens().get(4));
            watchVO.setGlucose(data.getBloodGlucose());
            watchVO.setSystolic(data.getHighValue());
            watchVO.setDiastolic(data.getLowValue());
            if (data.getBloodComposition() != null) {
                watchVO.setUricAcid(data.getBloodComposition().getUricAcid());
                watchVO.setTotalCholesterol(data.getBloodComposition().getTCHO());
                watchVO.setHighDensityLipoprotein(data.getBloodComposition().getHDL());
                watchVO.setLowDensityLipoprotein(data.getBloodComposition().getLDL());
                watchVO.setTriglycerides(data.getBloodComposition().getTAG());
            }
            return watchVO;
        } else {
            for (int i = 0; i < list.size(); i++) {

                String messageStr = list.get(i).getMessageStr();
                WatchMessage watchMessage = JSONObject.parseObject(messageStr, WatchMessage.class);
                WatchMessage.DeviceData data = watchMessage.getData();
                if (i == 0) {
                    watchVO.setHeartRate(data.getPpgs().get(4));
                    watchVO.setOxygen(data.getOxygens().get(4));
                }
                watchVO.setGlucose(data.getBloodGlucose());
                watchVO.setSystolic(data.getHighValue());
                watchVO.setDiastolic(data.getLowValue());
                if (data.getBloodComposition() != null) {
                    watchVO.setUricAcid(data.getBloodComposition().getUricAcid());
                    watchVO.setTotalCholesterol(data.getBloodComposition().getTCHO());
                    watchVO.setHighDensityLipoprotein(data.getBloodComposition().getHDL());
                    watchVO.setLowDensityLipoprotein(data.getBloodComposition().getLDL());
                    watchVO.setTriglycerides(data.getBloodComposition().getTAG());
                }
                if (watchVO.getGlucose() > 0) {
                    break;
                }
            }

        }
        return watchVO;
    }

    @Override
    public WatchVO getLastMessageByCustomerIdDate(Long customerId, String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
        Date date1 = null;
        try {
            date1 = sdf.parse(date);
        } catch (ParseException e) {

        }
        WatchVO watchVO = new WatchVO();
        List<SensorMessage> list = sensorMessageMapper.getLastMessageByCustomerIdDate(customerId, date1);
        if (list.isEmpty()) {
            return watchVO;
        } else if (list.size() == 1) {
            String messageStr = list.get(0).getMessageStr();
            WatchMessage watchMessage = JSONObject.parseObject(messageStr, WatchMessage.class);
            WatchMessage.DeviceData data = watchMessage.getData();
            List<Integer> ppgs = data.getPpgs();
            for (int j = ppgs.size()-1; j >= 0; j--) {
                if(ppgs.get(j)>0){
                    watchVO.setHeartRate(ppgs.get(j));
                    break;
                }
            }
            List<Integer> oxygens = data.getOxygens();
            for (int j = oxygens.size()-1; j >= 0; j--) {
                if(oxygens.get(j)>0){
                    watchVO.setOxygen(oxygens.get(j));
                    break;
                }
            }
            watchVO.setGlucose(data.getBloodGlucose());
            watchVO.setSystolic(data.getHighValue());
            watchVO.setDiastolic(data.getLowValue());
            if (data.getBloodComposition() != null) {
                watchVO.setUricAcid(data.getBloodComposition().getUricAcid());
                watchVO.setTotalCholesterol(data.getBloodComposition().getTCHO());
                watchVO.setHighDensityLipoprotein(data.getBloodComposition().getHDL());
                watchVO.setLowDensityLipoprotein(data.getBloodComposition().getLDL());
                watchVO.setTriglycerides(data.getBloodComposition().getTAG());
            }
            return watchVO;
        } else {
            for (int i = 0; i < list.size(); i++) {

                String messageStr = list.get(i).getMessageStr();
                WatchMessage watchMessage = JSONObject.parseObject(messageStr, WatchMessage.class);
                WatchMessage.DeviceData data = watchMessage.getData();
                if(watchVO.getHeartRate()==null){
                    List<Integer> ppgs = data.getPpgs();
                    for (int j = ppgs.size()-1; j >= 0; j--) {
                        if(ppgs.get(j)>0){
                            watchVO.setHeartRate(ppgs.get(j));
                            break;
                        }
                    }
                }
                if(watchVO.getOxygen()==null){
                    List<Integer> oxygens = data.getOxygens();
                    for (int j = oxygens.size()-1; j >= 0; j--) {
                        if(oxygens.get(j)>0){
                            watchVO.setOxygen(oxygens.get(j));
                            break;
                        }
                    }
                }
                if(watchVO.getGlucose() ==null||!(watchVO.getGlucose() > 0)){
                    watchVO.setGlucose(data.getBloodGlucose());
                    watchVO.setSystolic(data.getHighValue());
                    watchVO.setDiastolic(data.getLowValue());
                }

                if (data.getBloodComposition() != null) {
                    watchVO.setUricAcid(data.getBloodComposition().getUricAcid());
                    watchVO.setTotalCholesterol(data.getBloodComposition().getTCHO());
                    watchVO.setHighDensityLipoprotein(data.getBloodComposition().getHDL());
                    watchVO.setLowDensityLipoprotein(data.getBloodComposition().getLDL());
                    watchVO.setTriglycerides(data.getBloodComposition().getTAG());
                }
//                if (watchVO.getGlucose() > 0) {
//                    break;
//                }
            }

        }
        return watchVO;
    }

    /**
     * 获取上周健康数据，按天统计平均值
     * 
     * @param healthParam 健康参数，包含客户ID和传入时间
     * @return 健康数据，按周一至周日分组的平均值统计
     */
    @Override
    public HealthDatas getLastWeekHealthData(HeathParam healthParam) {
        // 获取传入时间对应的上周日期范围（周一至周日）
        LocalDateTime localDateTime = healthParam.getDate().toInstant().atZone(ZoneId.systemDefault())
                .toLocalDateTime();
        LocalDateTime startOfDay = localDateTime.toLocalDate().atStartOfDay();

        // 计算当前时间所在周的周一
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant()));
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        // 前一周的周一
        calendar.add(Calendar.DAY_OF_YEAR, -7);
        Date weekStart = calendar.getTime();
        // 前一周的周日
        calendar.add(Calendar.DAY_OF_YEAR, 6);
        Date weekEnd = calendar.getTime();

        // 获取上周的所有数据
        List<SensorMessage> sensorMessages = sensorMessageMapper.getDataByCustomerId(healthParam.getCustomerId(),
                weekStart, weekEnd);
        if (sensorMessages.isEmpty()) {
            return null;
        }

        // 初始化按天统计的数据结构
        String[] weekdays = { "周一", "周二", "周三", "周四", "周五", "周六", "周日" };
        // 存储每天的数据合计和计数
        double[][] dailyStats = new double[7][10]; // [天][血糖总和,血糖计数,高压总和,低压总和,血压计数,血氧总和,血氧计数,心率总和,心率计数]

        // 遍历所有数据，按天统计
        for (SensorMessage sensorMessage : sensorMessages) {
            String messageStr = sensorMessage.getMessageStr();
            WatchMessage watchMessage = JSONObject.parseObject(messageStr, WatchMessage.class);
            WatchMessage.DeviceData data = watchMessage.getData();
            Date tsStart = sensorMessage.getTsStart();

            // 计算是周几（0-6表示周一到周日）
            Calendar dataCalendar = Calendar.getInstance();
            dataCalendar.setTime(tsStart);
            int dayOfWeek = dataCalendar.get(Calendar.DAY_OF_WEEK) - 2;
            if (dayOfWeek < 0)
                dayOfWeek = 6; // Calendar.MONDAY=2，调整为0-6

            // 统计血糖数据
            if (!data.getBloodGlucose().equals(0.0d)) {
                dailyStats[dayOfWeek][0] += data.getBloodGlucose();
                dailyStats[dayOfWeek][1]++;
            }

            // 统计血压数据
            if (!data.getHighValue().equals(0)) {
                dailyStats[dayOfWeek][2] += data.getHighValue();
                dailyStats[dayOfWeek][3] += data.getLowValue();
                dailyStats[dayOfWeek][4]++;
            }

            // 统计血氧数据
            List<Integer> oxygens = data.getOxygens();
            for (Integer oxygen : oxygens) {
                if (oxygen > 0) {
                    dailyStats[dayOfWeek][5] += oxygen;
                    dailyStats[dayOfWeek][6]++;
                }
            }

            // 统计心率数据
            List<Integer> ppgs = data.getPpgs();
            for (Integer ppg : ppgs) {
                if (ppg > 0) {
                    dailyStats[dayOfWeek][7] += ppg;
                    dailyStats[dayOfWeek][8]++;
                }
            }
        }

        // 创建返回结果
        HealthDatas healthDatas = new HealthDatas();
        healthDatas.setBloodSugars(new ArrayList<>());
        healthDatas.setHeartRates(new ArrayList<>());
        healthDatas.setOxygens(new ArrayList<>());
        healthDatas.setBloodPressures(new ArrayList<>());

        // 生成按天统计的结果
        for (int i = 0; i < 7; i++) {
            // 血糖数据
            if (dailyStats[i][1] > 0) {
                TimeValueItem bloodSugar = new TimeValueItem();
                bloodSugar.setLabel(weekdays[i]);
                bloodSugar.setSortOrder(i);
                // 血糖保留1位小数
                bloodSugar.setValue1(Math.round((dailyStats[i][0] / dailyStats[i][1]) * 10) / 10.0);
                healthDatas.getBloodSugars().add(bloodSugar);
            }

            // 血压数据
            if (dailyStats[i][4] > 0) {
                TimeValueItem bloodPressure = new TimeValueItem();
                bloodPressure.setLabel(weekdays[i]);
                bloodPressure.setSortOrder(i);
                // 血压取整
                bloodPressure.setValue1(Math.round(dailyStats[i][2] / dailyStats[i][4]));
                bloodPressure.setValue2(Math.round(dailyStats[i][3] / dailyStats[i][4]));
                healthDatas.getBloodPressures().add(bloodPressure);
            }

            // 血氧数据
            if (dailyStats[i][6] > 0) {
                TimeValueItem oxygen = new TimeValueItem();
                oxygen.setLabel(weekdays[i]);
                oxygen.setSortOrder(i);
                // 血氧取整
                oxygen.setValue1(Math.round(dailyStats[i][5] / dailyStats[i][6]));
                healthDatas.getOxygens().add(oxygen);
            }

            // 心率数据
            if (dailyStats[i][8] > 0) {
                TimeValueItem heartRate = new TimeValueItem();
                heartRate.setLabel(weekdays[i]);
                heartRate.setSortOrder(i);
                // 心率取整
                heartRate.setValue1(Math.round(dailyStats[i][7] / dailyStats[i][8]));
                healthDatas.getHeartRates().add(heartRate);
            }
        }

        return healthDatas;
    }

    /**
     * 获取上月健康数据，按天统计平均值
     * 
     * @param healthParam 健康参数，包含客户ID和传入时间
     * @return 健康数据，按1日至月末分组的平均值统计
     */
    @Override
    public HealthDatas getLastMonthHealthData(HeathParam healthParam) {
        // 获取传入时间对应的上个月日期范围（1日至月末）
        LocalDateTime localDateTime = healthParam.getDate().toInstant().atZone(ZoneId.systemDefault())
                .toLocalDateTime();
        LocalDateTime startOfDay = localDateTime.toLocalDate().atStartOfDay();

        // 计算上个月的开始和结束日期
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(Date.from(startOfDay.atZone(ZoneId.systemDefault()).toInstant()));
        // 将日期设置为本月第一天
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        // 向前推一个月
        calendar.add(Calendar.MONTH, -1);
        // 获取上个月第一天
        Date monthStart = calendar.getTime();
        // 获取上个月最后一天
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date monthEnd = calendar.getTime();

        // 获取上个月的所有数据
        List<SensorMessage> sensorMessages = sensorMessageMapper.getDataByCustomerId(healthParam.getCustomerId(),
                monthStart, monthEnd);
        if (sensorMessages.isEmpty()) {
            return null;
        }

        // 获取上个月的天数
        int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        // 初始化按天统计的数据结构
        // 存储每天的数据合计和计数：[天][血糖总和,血糖计数,高压总和,低压总和,血压计数,血氧总和,血氧计数,心率总和,心率计数]
        double[][] dailyStats = new double[daysInMonth][9];

        // 遍历所有数据，按天统计
        for (SensorMessage sensorMessage : sensorMessages) {
            String messageStr = sensorMessage.getMessageStr();
            WatchMessage watchMessage = JSONObject.parseObject(messageStr, WatchMessage.class);
            WatchMessage.DeviceData data = watchMessage.getData();
            Date tsStart = sensorMessage.getTsStart();

            // 计算是当月第几天（0-天数-1）
            Calendar dataCalendar = Calendar.getInstance();
            dataCalendar.setTime(tsStart);
            int dayOfMonth = dataCalendar.get(Calendar.DAY_OF_MONTH) - 1;

            // 统计血糖数据
            if (!data.getBloodGlucose().equals(0.0d)) {
                dailyStats[dayOfMonth][0] += data.getBloodGlucose();
                dailyStats[dayOfMonth][1]++;
            }

            // 统计血压数据
            if (!data.getHighValue().equals(0)) {
                dailyStats[dayOfMonth][2] += data.getHighValue();
                dailyStats[dayOfMonth][3] += data.getLowValue();
                dailyStats[dayOfMonth][4]++;
            }

            // 统计血氧数据
            List<Integer> oxygens = data.getOxygens();
            for (Integer oxygen : oxygens) {
                if (oxygen > 0) {
                    dailyStats[dayOfMonth][5] += oxygen;
                    dailyStats[dayOfMonth][6]++;
                }
            }

            // 统计心率数据
            List<Integer> ppgs = data.getPpgs();
            for (Integer ppg : ppgs) {
                if (ppg > 0) {
                    dailyStats[dayOfMonth][7] += ppg;
                    dailyStats[dayOfMonth][8]++;
                }
            }
        }

        // 创建返回结果
        HealthDatas healthDatas = new HealthDatas();
        healthDatas.setBloodSugars(new ArrayList<>());
        healthDatas.setHeartRates(new ArrayList<>());
        healthDatas.setOxygens(new ArrayList<>());
        healthDatas.setBloodPressures(new ArrayList<>());

        // 生成按天统计的结果
        for (int i = 0; i < daysInMonth; i++) {
            // 血糖数据
            if (dailyStats[i][1] > 0) {
                TimeValueItem bloodSugar = new TimeValueItem();
                bloodSugar.setLabel((i + 1) + "日");
                bloodSugar.setSortOrder(i);
                // 血糖保留1位小数
                bloodSugar.setValue1(Math.round((dailyStats[i][0] / dailyStats[i][1]) * 10) / 10.0);
                healthDatas.getBloodSugars().add(bloodSugar);
            }

            // 血压数据
            if (dailyStats[i][4] > 0) {
                TimeValueItem bloodPressure = new TimeValueItem();
                bloodPressure.setLabel((i + 1) + "日");
                bloodPressure.setSortOrder(i);
                // 血压取整
                bloodPressure.setValue1(Math.round(dailyStats[i][2] / dailyStats[i][4]));
                bloodPressure.setValue2(Math.round(dailyStats[i][3] / dailyStats[i][4]));
                healthDatas.getBloodPressures().add(bloodPressure);
            }

            // 血氧数据
            if (dailyStats[i][6] > 0) {
                TimeValueItem oxygen = new TimeValueItem();
                oxygen.setLabel((i + 1) + "日");
                oxygen.setSortOrder(i);
                // 血氧取整
                oxygen.setValue1(Math.round(dailyStats[i][5] / dailyStats[i][6]));
                healthDatas.getOxygens().add(oxygen);
            }

            // 心率数据
            if (dailyStats[i][8] > 0) {
                TimeValueItem heartRate = new TimeValueItem();
                heartRate.setLabel((i + 1) + "日");
                heartRate.setSortOrder(i);
                // 心率取整
                heartRate.setValue1(Math.round(dailyStats[i][7] / dailyStats[i][8]));
                healthDatas.getHeartRates().add(heartRate);
            }
        }

        return healthDatas;
    }

    @Override
    public Boolean addmockData(Long customerId) {
        List<SensorMessage> lastMessageByCustomerId = sensorMessageMapper
                .getLastMessageByCustomerId(590118625319329792L);
        lastMessageByCustomerId.forEach(message -> {
            message.setId(PrimaryKeyIdUtils.getSysIdWorker().nextId());
            message.setCustomerId(customerId);
            Date date = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            // 在原始时间基础上加上5分钟
            calendar.add(Calendar.MINUTE, 5);
            // 获取加了5分钟后的时间
            Date fiveMinutesLater = calendar.getTime();
            message.setTsStart(date);
            message.setTsEnd(fiveMinutesLater);
            message.setCreateTime(DateUtils.getNowDate());
            sensorMessageMapper.insertSensorMessage(message);
        });
        return true;
    }
}
